package com.leyou.item.web;

import com.leyou.common.dto.SpecGroupDTO;
import com.leyou.common.dto.SpecParamDTO;
import com.leyou.item.entity.SpecGroup;
import com.leyou.item.entity.SpecParam;
import com.leyou.item.service.SpecGroupService;
import com.leyou.item.service.SpecParamService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("spec")
public class SpecController {

    private SpecGroupService groupService;
    private SpecParamService paramService;

    public SpecController(SpecGroupService groupService, SpecParamService paramService) {
        this.groupService = groupService;
        this.paramService = paramService;
    }

    /**
     * 根据商品分类查询规格组
     *
     * @param id 商品分类id
     * @return 规格组集合
     */
    @GetMapping("/groups/of/category")
    public ResponseEntity<List<SpecGroupDTO>> queryGroupByCategory(
            @RequestParam("id") Long id) {
        return ResponseEntity.ok(
                SpecGroupDTO.convertEntityList(
                        groupService.query().eq("category_id", id).list()
                )
        );

    }


    /**
     * 查询规格参数集合
     *
     * @param categoryId 分类id
     * @param groupId    规格组id
     * @param searching  是否搜索
     * @return 参数集合
     */
    @GetMapping("params")
    public ResponseEntity<List<SpecParamDTO>> querySpecParams(
            @RequestParam(value = "categoryId", required = false) Long categoryId,
            @RequestParam(value = "groupId", required = false) Long groupId,
            @RequestParam(value = "searching", required = false) Boolean searching
    ) {
        return ResponseEntity.ok(paramService.queryParams(categoryId, groupId, searching));
    }


    /**
     * 新增规格组
     *
     * @param groupDTO 规格组信息
     * @return 无
     */
    @PostMapping("group")
    public ResponseEntity<Void> saveGroup(@RequestBody SpecGroupDTO groupDTO) {
        // 新增规格组
        groupService.save(groupDTO.toEntity(SpecGroup.class));
        // 返回结果
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }


    /**
     * 修改规格组
     *
     * @param groupDTO 规格组信息
     * @return 无
     */
    @PutMapping("group")
    public ResponseEntity<Void> updateGroup(
            @RequestBody SpecGroupDTO groupDTO) {
        // 更新规格组
        groupService.updateById(groupDTO.toEntity(SpecGroup.class));
        // 返回结果
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }


    /**
     * 新增规格参数
     *
     * @param paramDTO 规格组信息
     * @return 无
     */
    @PostMapping("param")
    public ResponseEntity<Void> saveParam(@RequestBody SpecParamDTO paramDTO) {
        paramService.save(paramDTO.toEntity(SpecParam.class));
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 修改规格参数
     *
     * @param paramDTO 规格组信息
     * @return 无
     */
    @PutMapping("param")
    public ResponseEntity<Void> updateParam(@RequestBody SpecParamDTO paramDTO) {
        paramService.updateById(paramDTO.toEntity(SpecParam.class));
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 根据分类id查询规格组及组内参数
     *
     * @return 规格组集合
     */
    @GetMapping("/list")
    public ResponseEntity<List<SpecGroupDTO>> querySpecList(@RequestParam("id") Long categoryId) {
        return ResponseEntity.ok(groupService.querySpecList(categoryId));

    }

}
